import { createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import {MessagePlugin} from 'tdesign-vue-next';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: () => import("../layout/index.vue"),
        meta:{
            title: "👨‍🎓毕设选导师系统"
        },
        redirect: '/home',
        children:[
            {
                path: '/home',
                component: () => import("../views/HomePage/index.vue"),
                meta:{
                    title: "👨‍🎓毕设选导师系统"
                },
            },
            {
                path: '/my-mentor',
                component: () => import("../views/studentPages/MyMentor.vue"),
                meta:{
                    title: "👨‍🏫我的导师"
                },
            },
            {
                path: '/message',
                component: () => import("../views/commonPages/MyMessage.vue"),
                meta:{
                    title: "👻我的消息"
                }
            },
            {
                path: 'account-setting',
                component: () => import("../views/commonPages/AccounSetting.vue"),
                meta:{
                    title: "👾账户设置"
                }
            },
            {
                path: 'my-student-list',
                component: () => import("../views/teacherPages/myStudentList.vue"),
                meta:{
                    title: "👨‍🎓学生列表"
                }
            }
        ]
    },
    {
        path: '/login',
        component: () => import('../views/login/index.vue'),
        meta:{
            title: '🙈登录Login'
        }
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next)=>{
    // @ts-ignore
    //修改页面标题
    document.title = to.meta.title;
    if(sessionStorage.getItem("token") != null){
        //已登录
        next();
    }else{
        //未登录 防止栈溢出
        if(to.path === '/login'){
            next();
        }else{
            MessagePlugin.warning("请先登录！");
            next('/login');
        }
    }
})

// router.beforeEach((to, from, next)=>{
//     const store = useStore();
//     // @ts-ignore
//     //修改页面标题
//     document.title = to.meta.title;
//     if(store.getToken !== ''){
//         //已登录
//         next();
//     }else{
//         //未登录 防止栈溢出
//         if(to.path === '/user-login'){
//             next();
//         }else{
//             next('/user-login');
//         }
//     }
//
// })
export default router;
